﻿using System;
using SoftwareConsulting.BI11.NumericalMethods.DhbFunctionEvaluation;
using SoftwareConsulting.BI11.NumericalMethods.MatrixAlgebra;

namespace SoftwareConsulting.BI11.NumericalMethods.Regression
{
    /// <summary>
    /// Класс многочлена с оценкой ошибки
    /// </summary>
    public class EstimatedPolynomial : PolynomialFunction
    {	    
        private SymmetricMatrix _errorsMatrix;//матрица ошибок

        /// <summary>
        /// Конструктор класса
        /// </summary>
        /// <param name="coefficients">полиномиальные коэффициенты</param>
        /// <param name="errorsMatrix">матрица ошибок</param>
        public EstimatedPolynomial(double[] coefficients, SymmetricMatrix errorsMatrix) : 
            base(coefficients)
        {
            _errorsMatrix = errorsMatrix;
        }

        /// <summary>
        /// Возвращает ошибку для указанного значения
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        public double GetErrorAt(double x)
        {
            int n = this.Degree + 1;
            double[] errors = new double[n];
            errors[0] = 1;
            for (int i = 1; i < n; i++)
                errors[i] = errors[i - 1] * x;
            Vector errorVector = new Vector(errors);
            double result = 0;
            try
            {
                result = errorVector * (_errorsMatrix * errorVector);
            }
            catch (BIMatrixIllegalDimensionException) 
            {
                result = double.NaN;
            };            
            return Math.Sqrt(result);
        }
    }
}
